package com.army.study.service;

import com.army.study.database.BusComesDatabase;
import com.army.study.database.Form;
import com.army.study.utils.DatabaseUtils;
import com.army.study.widget.controller.FormController;
import com.army.study.widget.controller.FormControllerManager;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 定时器
 */
public class TimerAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
    private static final long SEND_PERIOD = 5000L;
    private DatabaseHelper helper = new DatabaseHelper(this);
    private OrmContext connect;
    public static Timer timer;

    @Override
    public void onStart(Intent intent) {
        HiLog.info(LABEL_LOG, "TimerAbility::onStart");
        connect = helper.getOrmContext("BusComesDatabase", "BusComesDatabase.db", BusComesDatabase.class);
        startTimer();
        super.onStart(intent);
    }

    // 卡片更新定时器，5秒更新一次
    private void startTimer() {
        timer = new Timer();
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        updateForms();
                    }
                },
                0,
                SEND_PERIOD);
    }

    /**
     * 更新全部卡片
     */
    private void updateForms() {
        // 从数据库中获取卡片信息
        OrmPredicates ormPredicates = new OrmPredicates(Form.class);
        List<Form> formList = connect.query(ormPredicates);
        // 更新时分秒
        if (formList.size() <= 0) {
            return;
        }
        for (Form form : formList) {
            // 遍历卡片列表更新卡片
            try {
                Long updateFormId = form.getFormId();

                FormControllerManager formControllerManager = FormControllerManager.getInstance(this);
                FormController formController = formControllerManager.getController(updateFormId);
                formController.updateFormData(updateFormId);
            } catch (Exception e) {
                // 删除不存在的卡片
                DatabaseUtils.deleteFormData(connect, form.getFormId());
                HiLog.error(LABEL_LOG, "onUpdateForm updateForm error");
            }
        }
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "TimerAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        HiLog.info(LABEL_LOG, "TimerAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
    }
}